package com.cgutech.netty;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Application {

    public static void main(String[] args) {
        if(args == null || args.length < 3) {
            System.out.println("参数不足：服务IP 客户端数量 每组最大连接数");
            return;
        }
        NettyClients clients = new NettyClients();
//        new Thread(()->{
            int count = Integer.parseInt(args[1]);
            List<DeviceConnectData> devices = new ArrayList<DeviceConnectData>();
            for(int i = 0; i < count; i++) {
                DeviceConnectData data = new DeviceConnectData();
                data.setUid(String.format("%08d", i + 1));
                data.setIp(args[0]);
                data.setPort(12001 + i / Integer.parseInt(args[2]));
                devices.add(data);
            }

            clients.start(devices);

//        }).start();

        do {
            Scanner scanner = new Scanner(System.in);
            String cmd = scanner.nextLine();
            if("quit".equals(cmd) || "exit".equals(cmd)) {
                clients.close();
                break;
            }
            if(cmd.equals("send")) {
                clients.send("Hello, I am Client!".getBytes());
            } else if(cmd.startsWith("send")) {
                try {
                    clients.send(cmd.substring(5, 13), cmd.substring(13).getBytes());
                } catch (Exception e) {
                    System.out.println("send指令异常");
                }
            }
        } while (true);
    }
}
